package StudentSystem.Mthod;

import StudentSystem.bean.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public abstract class StudentMethod {
    // 实现添加功能
    public static void add(ArrayList<Student> students) {
        try {
            System.out.println("--------------------------------------");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入学生id");
            String id = sc.next();
            if (checkId(students, id)) {
                System.out.println("您输入的id已存在，添加失败！！");
            } else {
                System.out.println("请输入学生姓名");
                String name = sc.next();
                System.out.println("请输入学生年龄");
                int age = sc.nextInt();
                if (age < 0 || age > 100) {
                    System.out.println("您输入的年龄不合法");
                } else {
                    System.out.println("请输入学生家庭住址");
                    String address = sc.next();
                    Student s = new Student(id, name, age, address);
                    writeFile(s);
                    students.clear();
                    System.out.println("添加成功！！");
                }
            }
        } catch (Exception e) {
            System.out.println("您输入的年龄有误！！");
            students.clear();
        }

    }

    // 实现删除功能
    public static void delete(ArrayList<Student> students) {
        System.out.println("--------------------------------------");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        if (checkId(students, id)) {
            students.removeIf(student -> id.equals(student.getId()));
            clearStudentData();
            writeFile(students);
            students.clear();
            System.out.println("删除成功！！");
        } else {
            System.out.println("您要删除的id不存在！！");
            students.clear();
        }

    }

    // 实现修改功能
    public static void replace(ArrayList<Student> students) {
        try {
            if (students.size() == 0) {
                readFile(students);
            }
            System.out.println("--------------------------------------");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要修改的学生id");
            String id = sc.next();
            if (checkId(students, id)) {
                while (true) {
                    System.out.println("请输入您要修改的内容1：姓名，2：年龄，3：家庭住址，0：返回初始菜单");
                    String select = sc.next();
                    switch (select) {
                        case "1" -> {
                            System.out.println("请输入要修改的学生姓名");
                            Student student = findStudent(students, id);
                            student.setName(sc.next());
                            clearStudentData();
                            writeFile(students);
                            students.clear();
                        }
                        case "2" -> {
                            System.out.println("请输入要修改的学生年龄");
                            int age = sc.nextInt();
                            if (age < 0 || age > 100) {
                                System.out.println("您输入的年龄不合法");
                                students.clear();
                            } else {
                                Student student = findStudent(students, id);
                                student.setAge(age);
                                clearStudentData();
                                writeFile(students);
                                students.clear();
                            }
                        }
                        case "3" -> {
                            System.out.println("请输入要修改的学生地址");
                            Student student = findStudent(students, id);
                            student.setAddress(sc.next());
                            clearStudentData();
                            writeFile(students);
                            students.clear();
                        }
                        case "0" -> {
                            students.clear();
                            return;
                        }
                        default -> {
                            System.out.println("您输入的选项不对！！请重新输入！！");
                            students.clear();
                        }
                    }
                }
            } else {
                System.out.println("您要修改的id不存在，请重新输入");
                students.clear();
            }
        } catch (Exception e) {
            System.out.println("您输入的年龄有误！！");
            students.clear();
        }
    }

    // 实现查询功能
    public static void printStudent(ArrayList<Student> students) {
        if (students.size() == 0) {
            System.out.println("当前无学生信息");
        } else {
            System.out.println("id\t\t\t姓名\t年龄");
        }
        for (Student student : students) {
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge());
        }
        students.clear();
    }

    // 清空文件内容
    public static void clearStudentData() {
        File file = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\StudentData.txt");
        try (FileWriter fw = new FileWriter(file)) {
            fw.write("");
            fw.flush();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 读取文件
    public static void readFile(ArrayList<Student> students) {
        File f = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\StudentData.txt");
        try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
            String str;
            while ((str = reader.readLine()) != null) {
                String[] newStr = str.split(",");
                students.add(new Student(newStr[0], newStr[1], Integer.parseInt(newStr[2]), newStr[3]));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 写入学生文件
    public static void writeFile(Student date) {
        // 写入文件
        File file = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\StudentData.txt");
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
            bw.write(date + "\n");
            bw.flush();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void writeFile(ArrayList<Student> students) {
        // 写入文件
        File file = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\StudentData.txt");
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
            for (Student student : students) {
                bw.write(student + "\n");
                bw.flush();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 查找对应id的学生对象
    public static Student findStudent(ArrayList<Student> students, String id) {
        for (Student student : students) {
            if (id.equals(student.getId())) {
                return student;
            }
        }
        return new Student();
    }

    // 检查id的唯一性
    public static boolean checkId(ArrayList<Student> students, String id) {
        for (Student student : students) {
            if (id.equals(student.getId())) {
                return true;
            }
        }
        return false;
    }
}
